package server;

import org.omg.CORBA.ContextList;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * @author yuanxp
 * @Date 2021/4/22 10:00
 */
public class Mapper {

    public Mapper(MappedHost defaultHost) {
        this.defaultHost = defaultHost;
    }

    private MappedHost defaultHost;

    public MappedHost getDefaultHost() {
        return defaultHost;
    }

    public void setDefaultHost(MappedHost defaultHost) {
        this.defaultHost = defaultHost;
    }

    protected static final class MappedContext extends MapElement<Context> {
        public volatile List<MappedWrapper> mappedWrappers;

        public MappedContext(String name,Context context) {
            super(name, context);
            mappedWrappers = new ArrayList<>();
        }
        public void addWrapper(MappedWrapper mappedWrapper) {
            mappedWrappers.add(mappedWrapper);
        }

        public MappedWrapper getWrapper(String url) {
            List<MappedWrapper> collect = mappedWrappers.stream().filter(m -> m.name.equals(url)).collect(Collectors.toList());
            return collect != null && collect.size() > 0 ? collect.get(0) : null;
        }


    }


    // ---------------------------------------------------- Wrapper Inner Class


    protected static class MappedWrapper extends MapElement<Wrapper> {

        public MappedWrapper(String name, Wrapper wrapper) {
            super(name, wrapper);
        }
    }


    protected static final class MappedHost extends MapElement<Host> {

        public List<MappedContext> contexts;

        public MappedHost(String name, Host host) {
            super(name, host);
            contexts = new ArrayList<>();
        }

        public void addContext(MappedContext mappedContext) {
            contexts.add(mappedContext);
        }

        public MappedContext getContext(String path) {
            List<MappedContext> collect = contexts.stream().filter(m -> m.name.equals(path)).collect(Collectors.toList());
            return collect != null && collect.size() > 0 ? collect.get(0) : null;
        }

        public void removeContext(String path) {

        }
    }

    protected abstract static class MapElement<T> {

        public final String name;
        public final T object;

        public MapElement(String name, T object) {
            this.name = name;
            this.object = object;
        }
    }

}
